package com.linewow.xhyy.nihaonew.activity.user;

import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.linewow.xhyy.hrxb.DemoHelper;
import com.linewow.xhyy.nihaonew.R;
import com.linewow.xhyy.nihaonew.base.BaseActivity;
import com.linewow.xhyy.nihaonew.entity.NiHaoGeneral;
import com.linewow.xhyy.nihaonew.model.AppConstants;
import com.linewow.xhyy.nihaonew.net.api.NiHaoOkgo;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;
import rx.schedulers.Schedulers;

/**
 * Created by LXR on 2016/10/28.
 *
 *
 */
public class ChangePwdActivity extends BaseActivity {
    @Bind(R.id.toolbar_back_title)
    TextView toolbarBackTitle;
    @Bind(R.id.toolbar_back_left)
    ImageView toolbarBackLeft;
    @Bind(R.id.toolbar_back_right_tv)
    TextView toolbarBackRightTv;
    @Bind(R.id.changepwd_old)
    EditText changepwdOld;
    @Bind(R.id.changepwd_new)
    EditText changepwdNew;
    @Bind(R.id.changepwd_new_sure)
    EditText changepwdNewSure;

    @Override
    public void initView() {
        super.initView();
        setContentView(R.layout.activity_changepwd);
        ButterKnife.bind(this);
        toolbarBackRightTv.setVisibility(View.VISIBLE);
        toolbarBackTitle.setText(getString(R.string.title_changepwd));
        clickHide();
    }


    @OnClick({R.id.toolbar_back_left, R.id.toolbar_back_right_tv})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.toolbar_back_left:
                finish();
                break;
            case R.id.toolbar_back_right_tv:
                String old=changepwdOld.getText().toString();
                String newPwd=changepwdNew.getText().toString();
                String newPwd1=changepwdNewSure.getText().toString();
                if(newPwd.equals(newPwd1)){
                    showLoading();
                    NiHaoOkgo.changePwdInfo(old,newPwd)
                            .subscribeOn(Schedulers.io())
                            .observeOn(AndroidSchedulers.mainThread())
                            .subscribe(new Action1<NiHaoGeneral<String>>() {
                                @Override
                                public void call(NiHaoGeneral<String> stringNiHaoGeneral) {
                                    dismissLoading();
                                    if(stringNiHaoGeneral.status){
                                        DemoHelper.getInstance().logoutUpdate();
                                        setResult(AppConstants.CONFIGURECHANGECODE);
                                        finish();
                                    }else{
                                        showCanDialog(stringNiHaoGeneral.info);
                                    }
                                }
                            }, new Action1<Throwable>() {
                                @Override
                                public void call(Throwable throwable) {
                                    Log.e(TAG, "错误信息" + throwable.getMessage());
                                    showToast(getString(R.string.message_no_internet));
                                }
                            });
                }
                break;
        }
    }


}
